<?php

namespace app\admin\controller;

use PHPExcel;
use app\admin\model\KuaiDiHelpOrderModel;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

class Order extends Base
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改（2006-2018） - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }

    public function getHelpOrderList()
    {
        if (empty($this->userId)) {
            $res['success'] = false;
            $res['desc'] = '登录态失效';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $page = input('page', 1);
        $size = input('size', 10);
        $userName = input('userName', '');
        $tel = input('tel', '');
        $code = input('code', '');
        $ordeo_no = input('ordeo_no', '');
        $option_id = input('option_id', 0);
        $start_time = input('start_time', '');
        $end_time = input('end_time', '');


        $model = new KuaiDiHelpOrderModel();
        $where = [];
        if ($tel) {
            $where['tel'] = ['like', '%' . $tel . '%'];
        }
        if ($userName) {
            $where['user_name'] = ['like', '%' . $userName . '%'];
        }
        if ($code) {
            $where['code'] = ['=', $code];
        }
        if ($option_id) {
            $where['option_id'] = ['=', $option_id];
        }
        if ($ordeo_no) {
            $where['ordeo_no'] = ['=', $ordeo_no];
        }
        $res = $model->getHelpOrderList($where, $start_time, $end_time, $page, $size);
        return json($res);
    }
    public function downHelpOrderList()
    {

        if (empty($this->userId)) {
            $res['success'] = false;
            $res['desc'] = '登录态失效';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $page = input('page', 1);
        $size = input('size', 10);
        $userName = input('userName', '');
        $tel = input('tel', '');
        $code = input('code', '');
        $ordeo_no = input('ordeo_no', '');
        $option_id = input('option_id', 0);
        $start_time = input('start_time', '');
        $end_time = input('end_time', '');


        $model = new KuaiDiHelpOrderModel();
        $where = [];
        if ($tel) {
            $where['tel'] = ['like', '%' . $tel . '%'];
        }
        if ($userName) {
            $where['user_name'] = ['like', '%' . $userName . '%'];
        }
        if ($code) {
            $where['code'] = ['=', $code];
        }
        if ($option_id) {
            $where['option_id'] = ['=', $option_id];
        }
        if ($ordeo_no) {
            $where['ordeo_no'] = ['=', $ordeo_no];
        }
        if (empty($where) && empty($start_time) && empty($end_time)) {
            $res['success'] = false;
            $res['desc'] = '请填写筛选条件';
            $res['errorcode'] = '-1';
            return json($res);
        }
        $list = $model->getHelpOrderListData($where, $start_time, $end_time);
        if ($list) {
            $obj = new PHPExcel();
            $obj->getProperties()->setTitle('帮取快递导出'); //设置标题
            // 设置当前sheet
            $obj->setActiveSheetIndex(0);
            // 列标
            $listData = ['A', 'B', 'C', 'D', 'E'];


            $obj->getActiveSheet()->getColumnDimension('A')->setWidth(20);
            $obj->getActiveSheet()->getColumnDimension('B')->setWidth(25);
            $obj->getActiveSheet()->getColumnDimension('C')->setWidth(25);
            $obj->getActiveSheet()->getColumnDimension('D')->setWidth(25);
            $obj->getActiveSheet()->getColumnDimension('E')->setWidth(20);

            // 填充第一行数据
            $obj->getActiveSheet()
                ->setCellValue($listData[0] . '1', '快递')
                ->setCellValue($listData[1] . '1', '快递联系人')
                ->setCellValue($listData[2] . '1', '快递联系电话')
                ->setCellValue($listData[3] . '1', '取件码')
                ->setCellValue($listData[4] . '1', '备注');
            // 填充第n(n>=2, n∈N*)行数据
            $length = count($list);
            for ($i = 0; $i < $length; $i++) {
                $obj->getActiveSheet()->getRowDimension($i + 2)->setRowHeight(20); //设置行高
                $obj->getActiveSheet()->setCellValue($listData[0] . ($i + 2), $list[$i]['option_name']); //将其设置为文本格式
                $obj->getActiveSheet()->setCellValue($listData[1] . ($i + 2), $list[$i]['user_name']);
                $obj->getActiveSheet()->setCellValue($listData[2] . ($i + 2), $list[$i]['tel'] . "\t");
                $obj->getActiveSheet()->setCellValue($listData[3] . ($i + 2), $list[$i]['code'] . "\t");
                $obj->getActiveSheet()->setCellValue($listData[4] . ($i + 2), $list[$i]['note'] . "\t");
            }

            // $obj->getDefaultStyle()->getAlignment()->setWrapText(TRUE);
            $obj->getActiveSheet()->getStyle('A1:E' . $length + 1)->getAlignment()->setWrapText(true); //自动换行
            $obj->getActiveSheet()->getStyle('A1:E' . $length + 1)->getFont()->setSize(14); //字体大小
            $obj->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $obj->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            // ob_end_clean();//清除缓冲区,避免乱码
            $fileName = '帮取快递导出' . date('YmdHis');
            // $fileName = iconv("utf-8", "gb2312", $fileName);
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx');
            header('Cache-Control: max-age=1');
            $objWriter = \PHPExcel_IOFactory::createWriter($obj, 'Excel2007');
            $objWriter->save(ROOT_PATH . 'public' . DIRECTORY_SEPARATOR . 'uploads/excel' . DIRECTORY_SEPARATOR . $fileName . '.xlsx');
            $res['success'] = true;
            $res['desc'] = '';
            $res['errorcode'] = '';
            $res['data']['url'] = get_http_type() . $_SERVER['HTTP_HOST'] . DIRECTORY_SEPARATOR . 'uploads/excel' . DIRECTORY_SEPARATOR . $fileName . '.xlsx';
            return json($res);
        } else {
            $res['success'] = false;
            $res['desc'] = '下载数据为空';
            $res['errorcode'] = '-1';
            return json($res);
        }
    }

    public function unLinkExcel()
    {
        if (empty($this->userId)) {
            $res['success'] = false;
            $res['desc'] = '登录态失效';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $fileName = input('fileName', '');
        if (empty($fileName)) {
            $res['success'] = false;
            $res['desc'] = '参数错误';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $realName = pathinfo($fileName, PATHINFO_BASENAME);
        if ($realName) {
            unlink(ROOT_PATH . 'public' . DIRECTORY_SEPARATOR . 'uploads/excel' . DIRECTORY_SEPARATOR . $realName);
        }
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        return json($res);
    }

    public function upHelpOrderStatus()
    {
        if (empty($this->userId)) {
            $res['success'] = false;
            $res['desc'] = '登录态失效';
            $res['errorcode'] = '200022';
            return json($res);
        }
        $status = input('status', -1);
        $helpOrderId = input('helpOrderId', 0);
        if (!in_array($status, [1, 2, 3]) || $helpOrderId <= 0) {
            $res['success'] = false;
            $res['desc'] = '参数错误';
            $res['errorcode'] = '200022';
            return json($res);
        }

        $updata['status'] = $status;
        $where['id'] = ['=', $helpOrderId];
        $model = new KuaiDiHelpOrderModel();
        $res = $model->upHelpOrderStatus($where, $updata);
        return json($res);
    }
}
